package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.xuanzheng.company.entity.Task;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.TaskService;
import com.xuanzheng.company.service.impl.TaskServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Collectors;

@WebServlet("/api/task/complete")
public class CompleteTaskServlet extends HttpServlet {
    private final TaskService taskService = new TaskServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        JsonObject result = new JsonObject();

        try {
            // 检查用户是否登录
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute("user");
            if (user == null) {
                result.addProperty("success", false);
                result.addProperty("message", "请先登录");
                out.print(gson.toJson(result));
                return;
            }

            // 获取请求体
            String requestBody = req.getReader().lines().collect(Collectors.joining());
            JsonObject requestJson = gson.fromJson(requestBody, JsonObject.class);
            String taskId = requestJson.get("taskId").getAsString();

            // 获取任务信息
            Task task = taskService.getTaskById(taskId);
            if (task == null) {
                result.addProperty("success", false);
                result.addProperty("message", "任务不存在");
                out.print(gson.toJson(result));
                return;
            }

            // 检查是否是任务的执行者
            if (!user.getId().equals(task.getEmployeeId())) {
                result.addProperty("success", false);
                result.addProperty("message", "您不是该任务的执行者");
                out.print(gson.toJson(result));
                return;
            }

            // 检查任务状态是否为派遣中(0)
            if (task.getStatus() != 0) {
                result.addProperty("success", false);
                result.addProperty("message", "任务状态不正确，无法完成");
                out.print(gson.toJson(result));
                return;
            }

            // 完成任务
            boolean success = taskService.completeTask(taskId);

            if (success) {
                result.addProperty("success", true);
                result.addProperty("message", "任务已标记为完成，等待确认");
            } else {
                result.addProperty("success", false);
                result.addProperty("message", "任务完成失败");
            }
        } catch (Exception e) {
            result.addProperty("success", false);
            result.addProperty("message", "系统错误: " + e.getMessage());
            e.printStackTrace();
        }

        out.print(gson.toJson(result));
    }
} 